package com.copa.l1604;

import java.util.*;

/**
 * @author copa
 * @createDate 2023-02-07 09:09
 * @function 哈希表法
 */
public class Test1 {

    public List<String> alertNames(String[] keyName, String[] keyTime) {
        if (keyName.length <= 2) {
            return new ArrayList<>();
        }
        List<String> result = new ArrayList<>();
        // 定义map为人名对应的分钟集合数
        Map<String, List<Integer>> map = new HashMap<>();
        // 先处理人名对应的分钟数
        for (int i = 0; i < keyName.length; i++) {
            List<Integer> minutes = map.getOrDefault(keyName[i], new ArrayList<>());
            // 虽然可以这样做，但效率慢，需要优化一小点
            // String[] timeSplit = keyTime[i].split(":");
            // int min = Integer.parseInt(timeSplit[0]) * 60 + Integer.parseInt(timeSplit[1]);
            // 直接这样算快了不少，所以以后开发要注意了，没有特别方便的情况下不要用split，效率大大降低
            int min = (keyTime[i].charAt(0) - '0') * 10 * 60 + (keyTime[i].charAt(1) - '0') * 60
                    + (keyTime[i].charAt(3) - '0') * 10 + (keyTime[i].charAt(4) - '0');
            minutes.add(min);
            map.put(keyName[i], minutes);
        }
        // 逐一判断
        for (Map.Entry<String, List<Integer>> entry : map.entrySet()) {
            List<Integer> times = entry.getValue();
            Collections.sort(times);
            for (int i = 2; i < times.size(); i++) {
                if (times.get(i) - times.get(i - 2) <= 60) {
                    result.add(entry.getKey());
                    break;
                }
            }
        }
        Collections.sort(result);
        return result;
    }

    public static void main(String[] args) {
        String[] keyName = {"leslie","leslie","leslie","clare","clare","clare","clare"};
        String[] keyTime = {"13:00","13:20","14:00","18:00","18:51","19:30","19:49"};
        List<String> result = new Test1().alertNames(keyName, keyTime);
        for (String s : result) {
            System.out.println(s);
        }
    }
}
